<?php
namespace App\Services;
use Exception,Log;
use App\Models\Usersauthority;
use App\Models\Memberorders;
use App\Models\Linkslists;
use App\Models\Linkslistsminapps;
use App\Models\Users;
use App\Models\Miniappsauthorization;
use App\Services\BaiduService;

class LinkscheckService
{
    public static function linksParamasExamine($request)
    {
        if (empty($request->title) || mb_strlen($request->title) <= 3) return ["status"=>"error","code"=>1001,"msg"=>"请填写3字以上外链名称！"];
        if ($request->platfrom_type == 1) {
            if(strpos($request->msg_url,'profile_ext') !== false) return ["status"=>"error","code"=>1001,"msg"=>"请勿输入公众号主页链接(微信不允许),请使用微信公众号文章！"];
            if(strpos($request->msg_url,'mp.weixin.qq.com/s') === false && strpos($request->msg_url,'plugin-private') === false) return ["status"=>"error","code"=>1001,"msg"=>"请输入已发布的微信公众号文章链接！"];
            if(strpos($request->wechat,',') !== false || mb_strlen($request->wechat) > 50) return ["status"=>"error","code"=>1001,"msg"=>"请输入正确的单个微信号！"];
        } elseif ($request->platfrom_type == 2 || $request->platfrom_type == 3) {
            if(strpos($request->msg_url,'profile_ext') !== false) return ["status"=>"error","code"=>1001,"msg"=>"请勿输入公众号主页链接(微信不允许),您可以直接添加公众号二维码图片！"];
            if (count($request->services) <= 0) return ["status"=>"error","code"=>1001,"msg"=>"请配置客服回复！"];
        } elseif ($request->platfrom_type == 4) {
            if (empty($request->appid)) return ["status"=>"error","code"=>1001,"msg"=>"请选择要跳转的微信小程序！"];
            if (strpos($request->path,'mp://') !== false || strpos($request->path,'#小程序') !== false) return ["status"=>"error","code"=>1001,"msg"=>"您填写的不是小程序路径,小程序路径获取方法请查看下方教程！"];
            if(mb_strlen($request->path) > 1024) return ["status"=>"error","code"=>1001,"msg"=>"小程序路径最长为1024字符,请修改小程序路径！"];
        } elseif ($request->platfrom_type == 5) {
            return ["status"=>"error","code"=>1001,"msg"=>"由于微信接口改版,本方法已无法使用!"];
        } elseif ($request->platfrom_type == 6) {
            $usersauthority = Usersauthority::getUsersAuthority($request->users_id);
            if(!empty($usersauthority) && $usersauthority->miniapp_third != 2) return ["status"=>"error","code"=>1001,"msg"=>"请联系天天外链客服开通权限 (因部分用户使用本链接推广违规充值、支付,故需先联系客服审核!)"];
            if (empty($request->miniapp["appid"]) || empty($request->miniapp["original_id"])) return ["status"=>"error","code"=>1001,"msg"=>"请输入您推广的小程序APPID和原始ID"];
            if (strpos($request->miniapp["path"],'mp://') !== false || strpos($request->miniapp["path"],'#小程序') !== false) return ["status"=>"error","code"=>1001,"msg"=>"您填写的不是小程序路径,小程序路径获取方法请查看下方教程！"];
            if(mb_strlen($request->miniapp["path"]) > 1024) return ["status"=>"error","code"=>1001,"msg"=>"小程序路径最长为1024字符,请修改小程序路径！"];
            if (strlen($request->miniapp["appid"]) != 18 || substr($request->miniapp["appid"],0,2) != "wx") return ["status"=>"error","code"=>1001,"msg"=>"请输入正确的小程序APPID！"];
            if (strlen($request->miniapp["original_id"]) != 15 || substr($request->miniapp["original_id"],0,3) != "gh_") return ["status"=>"error","code"=>1001,"msg"=>"请输入正确的小程序原始ID！"];
        } elseif ($request->platfrom_type == 7) {
            if (empty($request->nike) || empty($request->wechat)) return ["status"=>"error","code"=>1001,"msg"=>"微信昵称最长为35个字符,请修改微信昵称！"];
            if(mb_strlen($request->nike) > 35) return ["status"=>"error","code"=>1001,"msg"=>"微信昵称最长为35个字符,请修改微信昵称！"];
            if(strpos($request->wechat,',') !== false || mb_strlen($request->wechat) > 50) return ["status"=>"error","code"=>1001,"msg"=>"请输入正确的单个微信号！"];
        } elseif ($request->platfrom_type == 8 || $request->platfrom_type == 9 || $request->platfrom_type == 11 || $request->platfrom_type == 12) {
            $platfromTypeNames = [8=>"微信",9=>"微信群",11=>"小程序",12=>"公众号"];
            if ($request->miniapp_style == 1) {
                if (empty($request->nike)) return ["status"=>"error","code"=>1001,"msg"=>"请输入".$platfromTypeNames[$request->platfrom_type]."昵称！"];
                if (empty($request->describe)) return ["status"=>"error","code"=>1001,"msg"=>"请输入".$platfromTypeNames[$request->platfrom_type]."描述！"];
            }
            if(mb_strlen($request->describe) > 30) return ["status"=>"error","code"=>1001,"msg"=>$platfromTypeNames[$request->platfrom_type]."描述不能超过30个字,请修改".$platfromTypeNames[$request->platfrom_type]."描述！"];
            if(mb_strlen($request->nike) > 35) return ["status"=>"error","code"=>1001,"msg"=>$platfromTypeNames[$request->platfrom_type]."昵称最长为35个字,请修改".$platfromTypeNames[$request->platfrom_type]."昵称/名称！"];
            if(mb_strlen($request->qrcode_notice) > 30) return ["status"=>"error","code"=>1001,"msg"=>$platfromTypeNames[$request->platfrom_type]."底部操作提示不能错过30个字,请修改底部操作提示！"];
            if (count($request->qrcodeLists) <= 0) return ["status"=>"error","code"=>1001,"msg"=>"请上传".$platfromTypeNames[$request->platfrom_type]."二维码！"];
            if(strpos($request->wechat,',') !== false || strpos($request->wechat,'，') !== false || mb_strlen($request->wechat) > 50) return ["status"=>"error","code"=>1001,"msg"=>"请输入正确的单个微信号！"];
        } elseif ($request->platfrom_type == 13) {
            if(strpos($request->msg_url,'https://') === false) return ["status"=>"error","code"=>1001,"msg"=>"微信小程序只支持Https链接！"];
            if(strpos($request->wechat,',') !== false || mb_strlen($request->wechat) > 50) return ["status"=>"error","code"=>1001,"msg"=>"请输入正确的单个微信号！"];
        }
        $medicalCheck = BaiduService::textMedicalCheck($request->title.$request->describe.$request->nike);
        if ($medicalCheck["code"] == 2) return ['status'=>'error',"code"=>1005,'msg'=>'链接不能体现医疗、金融、理财、免费等宣传内容,因为会被微信封禁；建议您更换一下文案! 命中关键词 "'.$medicalCheck["keyword"].'"。'];
        return ["status"=>"success"];
    }

    public static function baiduSafeCheck($request)
    {
        $baiduCheck = BaiduService::textSimpleCheck($request->title.$request->describe.$request->nike);
        if ($baiduCheck == 2) {
            // 禁用账户
            Users::updateDsiable($request->users_id,2);
            Linkslists::linksListsDisable($request->users_id,$request->links_id);
            return ['status' => 'error','msg' => '外链内容违规,账号已被禁用；请联系微信客服为您处理！'];
        }
        return ["status"=>"success"];
    }

    public static function getUsersLinksPv($users_id)
    {
        $member = Memberorders::getLastLinksOrders($users_id);
        $end_time = strtotime("+60 minutes");
        $limit_pv = 100;
        $limit_status = 1;
        if (!empty($member) && $member->end_time >= time()) {
            $linkscount = Linkslists::usersLinksListsCount($users_id);
            if ($member->product_id == 2) {
                $limit_pv = 50000;
                $limit_status = 1;
            } elseif ($member->product_id == 3) {
                $limit_pv = 500000;
                $limit_status = 1;
            } elseif ($member->product_id == 4) {
                $limit_pv = 1000000;
                $limit_status = 2;
            } else {
                $limit_pv = 100;
                $limit_status = 1;
            }
            $end_time = $member->end_time;
            if ($member->product_id == 2 && $linkscount >= 3) return ['status'=>'error','msg'=>'外链接数量超过限额,请删除现有外链或升级为高级版！'];
            if ($member->product_id == 3 && $linkscount >= 100) return ['status'=>'error','msg'=>'外链接数量超过限额,请删除现有外链或联系客服提升数量！'];
            if ($member->product_id == 4 && $linkscount >= 200) return ['status'=>'error','msg'=>'外链接数量超过限额,请删除现有外链重新创建！'];
        } else {
            $linkscount = Linkslists::usersAllLinksListsCount($users_id);
            if ($linkscount >= 3) return ['status'=>'error','msg'=>'非会员累计创建外链数量不可超过3个,请开通VIP增加外链数量！'];
        }
        return ['status'=>'success','end_time'=>$end_time,'limit_pv'=>$limit_pv,'limit_status'=>$limit_status];
    }

    public static function getLinksListsMinapps($request,$links_id,$limit_pv,$status)
    {
        if (in_array($request->platfrom_type, [7,10])) {
            $original_id = $appid = $path = "";
            $miniapp_type = 2;
            $status = 1;
        } else {
            if (empty($request->appid)) {
                $minapps = Linkslistsminapps::getUseableMinapps($request->users_id);
                if (empty($minapps)) {
                    $usersauthority = Usersauthority::getUsersAuthority($request->users_id);
                    if (!empty($usersauthority) && $usersauthority->miniapp_style == 2) return ['status'=>'error','msg'=>'当前账号无法使用公用小程序，请上架独家小程序或者使用多悦数据进行推广，具体原因查看天天外链公用小程序说明。','code'=>5005];
                    if ($limit_pv > 1000) {
                        if ($request->platfrom_type == 1){
                            return ["status"=>"error","msg"=>"付费用户推广公众号需使用独家小程序,请联系客服部署独家小程序！",'code'=>1001];
                        } elseif (in_array($request->platfrom_type, [2,3])){
                            $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(4);
                            if (empty($minapps)) return ["status"=>"error","msg"=>"客服模式当前没有可用的小程序,请联系客服添加！",'code'=>1001];
                        } elseif (in_array($request->platfrom_type, [8,9,11,12,13])){
                            $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(3);
                            if (empty($minapps)) return ["status"=>"error","msg"=>"二维码模式当前没有可用的小程序,请联系客服添加！",'code'=>1001];
                        } elseif ($request->platfrom_type == 6){
                            $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(5);
                            if (empty($minapps)) return ["status"=>"error","msg"=>"三方小程序模式当前没有可用的小程序,请联系客服添加！",'code'=>1001];
                        } else {
                            $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(1);
                        }
                    } else {
                        $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(6);
                    }
                }
                if (empty($minapps)) return ["status"=>"error","msg"=>"当前没有可用的小程序,请联系客服！",'code'=>1001];
            } else {
                $minapps = Linkslistsminapps::getLinksListsMinappsByAppid($request->appid);

                if (!empty($minapps)) {
                    if ($minapps->users_id != $request->users_id){
                        if(in_array($request->platfrom_type, [1]) && $limit_pv > 1000) return ['status'=>'error','msg'=>'推广公众号必须使用独家小程序,请添加独家小程序！','code'=>1001];
                        if(!in_array($minapps->users_id,[5,12])) return ['status'=>'error','msg'=>'小程序不存在或已删除,请选择正确的小程序！','code'=>1001];
                    }
                    if ($request->platfrom_type == 4){
                        if (preg_match('/[\x{4e00}-\x{9fa5}]/u',$request->path)) {
                            return ['status'=>'error','msg'=>"微信小程序路径不能包含中文!",'code'=>1001];
                        }
                        // if ($minapps->miniapp_type == 2) {
                        //     $accessToken = MiniappService::miniappAccessToken($request->appid,$minapps->secret);
                        //     if (isset($accessToken["access_token"])) {
                        //         $access_token = $accessToken["access_token"];
                        //     } else {
                        //         return ['status'=>'error','msg'=>'小程序密钥错误，请检查小程序是否正确！','code'=>1001];
                        //     }
                        // } elseif ($minapps->miniapp_type == 4) {
                        //     $miniapp = Miniappsauthorization::getMiniappAuthorization($request->appid,["wxea6913abfa9210de"]);
                        //     $access_token = WechatopenService::getMiniProgramAccessToken($miniapp->component_appid,$miniapp->authorizer_appid,$miniapp->authorizer_refresh_token);
                        // }
                        // $pathParamas = explode("?", $request->path);
                        // $path = isset($pathParamas[0]) ? $pathParamas[0] : "";
                        // $pathquery = isset($pathParamas[1]) ? $pathParamas[1] : "";
                        // $generate = MiniappService::urlSchemeCreat($access_token,$path,$pathquery);
                        // if ($generate["status"] == "error") {
                        //     if ($generate["code"] == 40212) return ['status'=>'error','msg'=>"小程序路径错误,参数只支持数字、英文、及部分特殊字符！",'code'=>1001];
                        //     if ($generate["code"] == 40165) return ['status'=>'error','msg'=>"小程序路径错误,请填写正确的小程序路径!",'code'=>1001];
                        //     return ['status'=>'error','msg'=>$generate['msg'],'code'=>1001];
                        // }
                    }
                } else {
                    if ($request->platfrom_type == 4) return ['status'=>'error','msg'=>'小程序不存在或已删除,请选择正确的小程序！','code'=>1001];
                }
                // 原来的公用小程序被封禁/删除；换新的公用小程序
                if (empty($minapps) || $minapps->disable == 2) {
                    $usersauthority = Usersauthority::getUsersAuthority($request->users_id);
                    if (!empty($usersauthority) && $usersauthority->miniapp_style == 2) return ['status'=>'error','msg'=>'当前账号无法使用公用小程序，请上架独家小程序或者使用多悦数据进行推广，具体原因查看天天外链公用小程序说明。','code'=>5005];
                    if ($limit_pv > 1000) {
                        if ($request->platfrom_type == 1){
                            return ["status"=>"error","msg"=>"付费用户推广公众号需使用独家小程序,请联系客服部署独家小程序！",'code'=>1001];
                        } elseif (in_array($request->platfrom_type, [2,3])){
                            $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(4);
                            if (empty($minapps)) return ["status"=>"error","msg"=>"客服模式当前没有可用的小程序,请联系客服添加！",'code'=>1001];
                        } elseif (in_array($request->platfrom_type, [8,9,11,12,13])){
                            $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(3);
                            if (empty($minapps)) return ["status"=>"error","msg"=>"二维码模式当前没有可用的小程序,请联系客服添加！",'code'=>1001];
                        } elseif ($request->platfrom_type == 6){
                            $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(5);
                            if (empty($minapps)) return ["status"=>"error","msg"=>"三方小程序模式当前没有可用的小程序,请联系客服添加！",'code'=>1001];
                        } else {
                            $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(1);
                        }
                    } else {
                        $minapps = Linkslistsminapps::getPlatfromUseableDefaultMinapps(6);
                    }
                }
            }
            if (empty($minapps)) return ["status"=>"error","msg"=>"当前没有可用的小程序,请联系客服！",'code'=>1001];
            if (in_array($request->platfrom_type, [1,2,3,6,8,9,11,12,13,14,15,16])) {
                $path = "/pages/index/request?id=".$links_id; 
                if ($minapps->scene == 1) return ["status"=>"error","msg"=>"您自己开发的小程序不支持本模式,具体请联系微信客服！",'code'=>1001];
            } else {
                $path = $request->path; 
            }
            $original_id = $minapps->original_id;
            $appid = $minapps->appid;
            $miniapp_type = $minapps->miniapp_type;
            $status = $minapps->status;
        }
        return ['status'=>'success','original_id'=>$original_id,'appid'=>$appid,'miniapp_type'=>$miniapp_type,'appid'=>$appid,'path'=>$path,'status'=>$status];
    }
}
